/**
 * 
 */
package util;

import android.os.Handler;
import android.os.Message;

import core.KrnResponse;
import listener.OnPostListener;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author Crystalline
 * @GiteePage https://gitee.com/crystallineDW/krn
 */
public class PostUtil {

	private OnPostListener LOCALlistener;

	public String Post_MainThread(final OkHttpClient HttpClient,final String url,final Map<String,String> post_map) {
		
		RequestBody body = null;

        if(post_map!=null)
        {
        	FormBody.Builder post_data = new FormBody.Builder();
        	
        	for(Map.Entry<String, String> entry : post_map.entrySet()){
        	    String mapKey = entry.getKey();
        	    String mapValue = entry.getValue();
        	    post_data.add(mapKey,mapValue); 
        	}
        	body = post_data.build();  	
        }
        
        try {
			Request request = new Request.Builder()
					.url(url)
					.post(body)
					.build();

			Call call = HttpClient.newCall(request);
			Response response = call.execute();
			return response.body().string();
        }
        catch(Exception e)
        {
			return e.toString();
        }
		
	}
	
	
	public void Post_ChildThread(final OkHttpClient HttpClient,final String url,final Map<String,String> post_map,final OnPostListener listener) {

		LOCALlistener = listener;

		RequestBody body = null;

        if(post_map!=null)
        {
        	FormBody.Builder post_data = new FormBody.Builder();
        	
        	for(Map.Entry<String, String> entry : post_map.entrySet()){
        	    String mapKey = entry.getKey();
        	    String mapValue = entry.getValue();
        	    post_data.add(mapKey,mapValue); 
        	}
        	body = post_data.build();  	
        }
        
        try {
        	 Request request1 = new Request.Builder()
                     .url(url)
                     .post(body)
                     .build();
        	 Call call = HttpClient.newCall(request1);
        	    call.enqueue(new Callback() {
        	        @Override
        	        public void onFailure(Call call, IOException e) {



							Message msg = new Message();
							msg.what = 0;
							msg.obj = e;
							handler.sendMessage(msg);

        	        }




        	        @Override
        	        public void onResponse(Call call, Response response) throws IOException {


							Message msg = new Message();
							msg.what = 1;
							msg.obj = new KrnResponse(response);
							handler.sendMessage(msg);

        	        }
        	    });
        }
        catch(Exception e)
        {
        	listener.onPost_Failed(e);
        }
		
		
		
		
	}

	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what){
				case 0:
					//error
					IOException e = (IOException) msg.obj;
					LOCALlistener.onPost_Failed(e);
					break;
				case 1:
					//ok
					KrnResponse out = (KrnResponse) msg.obj;
					LOCALlistener.onPost_Success(out);
					break;
			}
		}
	};

}
